package com.wecheir.controller;


import com.wecheir.entity.Echarts.LastActivity;
import com.wecheir.entity.vo.LoginEchats;
import com.wecheir.service.CreditService;
import com.wecheir.service.LoginDataService;
import com.wecheir.service.UserService;
import com.wecheir.utils.DateUtil;
import com.wecheir.utils.ResponseUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

/**
 * 用户登录后数据的操作controller层
 */
@Controller
@RequestMapping("/app")
public class LoginDataController {

    @Resource
    LoginDataService loginDataService;

    @Resource
    CreditService creditService;

    @Resource
    UserService userService;




//    /**
//     * 获取最近七天的登录次数
//     * @return
//     */
//    @PostMapping("/datacharts")
//    @ResponseBody
//    public ResponseUtils datacharts(){
//        ResponseUtils responseEntity=new ResponseUtils();
//        List<LoginEchats> loginEchatsList=loginDataService.getLoginDataEcharts();
//
//        //取一周的数据
//        String[] createDateArray=new String[loginEchatsList.size()];
//        int[] dataArray=new int[loginEchatsList.size()];
//        LastActivity lastActivity=new LastActivity();//存储最近访问数据的实体
//
//        for (int i=0;i<loginEchatsList.size();i++){
//            //获取登录时间
//            if(null==loginEchatsList.get(i).getCreateDate()){
//                createDateArray[i]="该日期没有访问";
//            }else{
//                createDateArray[i]= DateUtil.getStringDateShort(loginEchatsList.get(i).getCreateDate());
//            }
//            dataArray[i]=loginEchatsList.get(i).getCount();
//        }
//        lastActivity.setCreateDateArray(createDateArray);
//        lastActivity.setDataArray(dataArray);
//        return ResponseUtils.setSuccess(lastActivity);
//
//    }

    /**
     * 获取最近七天的登陆次数，id标识身份
     * @param id
     * @return
     */
    @PostMapping("/datacharts")
    @ResponseBody
    public ResponseUtils datacharts(String id){

        //查询数据
        List<LoginEchats> loginEchatsList=loginDataService.getLoginDataEcharts(id);


        List<Date> dateList = DateUtil.getPastWeek(new Date());
        String[] loginDate = DateUtil.toStringArr(dateList);
        int[] loginTimes = new int[7];
        LastActivity lastActivity=new LastActivity();//存储最近访问数据的实体


        for(LoginEchats loginEchats : loginEchatsList){
            //对每一个有访问的日期进行存值
            int index = -1;

            for(int i = 0; i<7; i++){
                if(loginDate[i].equals(DateUtil.getStringDateWithColon(loginEchats.getCreateDate()))){
                    index = i;
                    break;
                }
            }

            if(index == -1)
                continue;

            loginTimes[index] = loginEchats.getCount();
        }

        lastActivity.setLoginDate(loginDate);
        lastActivity.setLoginTimes(loginTimes);
        return ResponseUtils.setSuccess(lastActivity);
    }
}
